﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using BusinessLogicLayer;
using DataAccessLayer;
//using DataAccessLayer.Reports;

namespace PresentationLayer
{
    public partial class GenerateReport : System.Web.UI.Page
    {
        String generatedBy;
        String filterBy;
        int fMonth, sMonth, tMonth, fYear, sYear, tYear;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        int roleId = Convert.ToInt32(list[6]);
                        if (roleId < 6 || roleId > 7)
                        {
                            Response.Redirect("/AccessDenied.aspx");
                        }
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion


                if (!IsPostBack)
                {
                    bindYear(ddlYear1);
                    bindYear(ddlYear2);
                    bindYear(ddlYear3);

                    bindMonth(ddlMonth1);
                    bindMonth(ddlMonth2);
                    bindMonth(ddlMonth3);

                    ddlMonth2.Enabled = false;
                    ddlMonth3.Enabled = false;
                    ddlYear2.Enabled = false;
                    ddlYear3.Enabled = false;
                }
            }
            catch (Exception ex)
            { 
            
            }
            
        }

        private void bindMonth(DropDownList ddlM)
        {
            ddlM.Items.Clear();
            ListItem lt = new ListItem();
            for (int i = 1; i <= 12; i++)
            {
                lt = new ListItem();
                lt.Text = Convert.ToDateTime(i.ToString() + "/1/1900").ToString("MMMM");
                lt.Value = i.ToString();
                ddlM.Items.Add(lt);
            }
            ddlM.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
        }

        private void bindYear(DropDownList ddl)
        {
            ddl.Items.Clear();
            ListItem lt = new ListItem();

            for (int i = DateTime.Now.Year; i >= 1950; i--)
            {
                lt = new ListItem();
                lt.Text = i.ToString();
                lt.Value = i.ToString();
                ddl.Items.Add(lt);
            }
            ddl.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;
        }

        protected void ddlNumOfMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlNumOfMonth.SelectedValue == "3")
            {
                ddlMonth2.Enabled = true;
                ddlMonth3.Enabled = true;
                ddlYear2.Enabled = true;
                ddlYear3.Enabled = true;
            }
            else if (ddlNumOfMonth.SelectedValue == "2")
            {
                ddlMonth2.Enabled = true;
                ddlMonth3.Enabled = false;
                ddlYear2.Enabled = true;
                ddlYear3.Enabled = false;
            }
            else
            {
                ddlMonth2.Enabled = false;
                ddlMonth3.Enabled = false;
                ddlYear2.Enabled = false;
                ddlYear3.Enabled = false;
            }
        }

        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                if(rdoCategory.Checked == true)
            {
                generatedBy = rdoCategory.Text;
            }
            else
            {
                generatedBy = rdoDepartment.Text;
            }
            if(rdoQty.Checked == true)
            {
                filterBy = rdoQty.Text;
            }
            else
            {
                filterBy = rdoPrice.Text;
            }
            
            if (ddlNumOfMonth.SelectedItem.Text == "1")
            {
                fMonth = Convert.ToInt32(ddlMonth1.SelectedValue);
                fYear = Convert.ToInt32(ddlYear1.SelectedValue);
                Response.Redirect("ReportViewer.aspx?num="+ ddlNumOfMonth.SelectedItem.Text +
                    "&generatedBy=" + generatedBy + "&filterBy=" + filterBy + "&fm="+fMonth+ "&fy=" + fYear);
            }
            else if (ddlNumOfMonth.SelectedItem.Text == "2")
            {
                fMonth = Convert.ToInt32(ddlMonth1.SelectedValue);
                fYear = Convert.ToInt32(ddlYear1.SelectedValue);
                sMonth = Convert.ToInt32(ddlMonth2.SelectedValue);
                sYear = Convert.ToInt32(ddlYear2.SelectedValue);

                Response.Redirect("ReportViewer.aspx?num=" + ddlNumOfMonth.SelectedItem.Text + 
                    "&generatedBy=" + generatedBy + "&filterBy=" + filterBy + "&fm=" + fMonth + "&fy=" + fYear +
                    "&sm=" + sMonth + "&sy=" + sYear);

            }
            else
            {
                fMonth = Convert.ToInt32(ddlMonth1.SelectedValue);
                fYear = Convert.ToInt32(ddlYear1.SelectedValue);
                sMonth = Convert.ToInt32(ddlMonth2.SelectedValue);
                sYear = Convert.ToInt32(ddlYear2.SelectedValue);
                tMonth = Convert.ToInt32(ddlMonth3.SelectedValue);
                tYear = Convert.ToInt32(ddlYear3.SelectedValue);
                Response.Redirect("ReportViewer.aspx?num=" + ddlNumOfMonth.SelectedItem.Text +
                    "&generatedBy=" + generatedBy + "&filterBy=" + filterBy + "&fm=" + fMonth + "&fy=" + fYear +
                    "&sm=" + sMonth + "&sy=" + sYear + "&tm=" + tMonth + "&ty=" + tYear);
            }           
          
        }catch (Exception ex)
            { 
            
            }
        }
            
    }
}